import React, { Component, lazy, Suspense } from 'react'
import { connect } from 'react-redux'
import { Layout } from 'antd';
import { Route, Redirect, Switch } from 'react-router-dom'

import './index.css'
// import Route from '../TraRoute'

class TraContent extends Component {

    render() {
        let { menus, location } = this.props
        //获取查询参数，并加入当前路径
        let qs = 'redirect=' + location.pathname
        if (location.search !== '') {
            qs += '&' + location.search.substr(1)
        }
        return (
            <Layout.Content className="main-content">
                <Suspense>
                    <Switch>
                        {menus.map(menu => <Route key={menu.menuId} path={'/main/' + menu.path} component={lazy(() => import('../../pages/' + menu.component))} />)}
                        <Redirect to={menus && menus.length ? "/main/index" : '/login?' + qs} />
                    </Switch>
                </Suspense>
            </Layout.Content>
        )
    }
}

const mapStateToProps = (state) => {
    let menus = state.menus.filter(menu => menu.menuType === 'C')
    return { menus }
}

const mapDispatchToProps = {
}

export default connect(mapStateToProps, mapDispatchToProps)(TraContent)
